package com.ruoyi.web.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.mapper.MesConfigMapper;
import com.ruoyi.web.service.*;
import com.ruoyi.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/quality")
public class MesQualityController extends BaseController {
    @Resource
    private MesQualityService mesQualityService;
    @Resource
    private MesQualityUserService mesQualityUserService;
    @Resource
    private MesQualityWorkService mesQualityWorkService;
    @Resource
    private MesConfigMapper configMapper;

    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody Map<String, String> params) {
        startPage(params);
        String code = params.get("code");
        String machineId = params.get("machineId");
        String disposeType = params.get("disposeType");
        String status = params.get("status");
        return getDataTable(mesQualityService.list(new QueryWrapper<MesQuality>().lambda()
                .like(StringUtils.isNotEmpty(code), MesQuality::getCode, code)
                .like(StringUtils.isNotEmpty(machineId), MesQuality::getMachineId, machineId)
                .like(StringUtils.isNotEmpty(disposeType), MesQuality::getDisposeType, disposeType)
                .eq(StringUtils.isNotEmpty(status), MesQuality::getStatus, status)
                .orderByDesc(MesQuality::getCreateTime)
        ));
    }

    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody MesQuality mesQuality) {

        // 新增
        if (mesQuality.getId() == null) {
            mesQuality.setId(UUID.randomUUID().toString().replace("-", ""));

            // 生成质量处理单编号
            Map<String, Object> map = new HashMap<>();
            map.put("columnName", "code");
            map.put("tableName", "mes_quality");
            int maxNum = configMapper.getMouthMaxId(map);
            String code = PinYinUtils.getCodeNormal(maxNum, "ZLCL");

            mesQuality.setCode(code);//供应商编号
            mesQuality.setCreateTime(DateUtils.getNowDate());
            mesQuality.setCreateBy(SecurityUtils.getUsername());

            if (mesQuality.getMesQualityUserList() != null) {
                mesQuality.getMesQualityUserList().forEach(e -> {
                    e.setQualityId(mesQuality.getId());
                });
            }
            if (mesQuality.getMesQualityWorkList() != null) {
                mesQuality.getMesQualityWorkList().forEach(e -> {
                    e.setQualityId(mesQuality.getId());
                });
            }
        }
        // 修改
        else {
            mesQuality.setUpdateTime(DateUtils.getNowDate());
            mesQuality.setUpdateBy(SecurityUtils.getUsername());
        }
        mesQualityService.saveOrUpdate(mesQuality);
        if (mesQuality.getMesQualityUserList() != null) {
            mesQualityUserService.remove(new QueryWrapper<MesQualityUser>().lambda().eq(MesQualityUser::getQualityId, mesQuality.getId()));
            mesQualityUserService.saveBatch(mesQuality.getMesQualityUserList());
        }
        if (mesQuality.getMesQualityWorkList() != null) {
            mesQualityWorkService.remove(new QueryWrapper<MesQualityWork>().lambda().eq(MesQualityWork::getQualityId, mesQuality.getId()));
            mesQualityWorkService.saveBatch(mesQuality.getMesQualityWorkList());
        }
        return AjaxResult.success("添加成功", mesQuality.getId());
    }

    @PostMapping(value = "/getById")
    public AjaxResult getById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        MesQuality mesQuality = mesQualityService.getById(id);
        List<MesQualityUser> mesQualityUserList = mesQualityUserService.list(new QueryWrapper<MesQualityUser>().lambda().eq(MesQualityUser::getQualityId, mesQuality.getId()));
        List<MesQualityWork> mesQualityWorkList = mesQualityWorkService.list(new QueryWrapper<MesQualityWork>().lambda().eq(MesQualityWork::getQualityId, mesQuality.getId()));
        mesQuality.setMesQualityUserList(mesQualityUserList);
        mesQuality.setMesQualityWorkList(mesQualityWorkList);

        return AjaxResult.success("查询成功", mesQuality);
    }

    @PostMapping(value = "/deleteById")
    public AjaxResult deleteById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        mesQualityService.removeById(id);
        mesQualityUserService.remove(new QueryWrapper<MesQualityUser>().lambda().eq(MesQualityUser::getQualityId, id));
        mesQualityWorkService.remove(new QueryWrapper<MesQualityWork>().lambda().eq(MesQualityWork::getQualityId, id));
        return AjaxResult.success("删除成功");
    }
}
